import { createRouter, createWebHistory } from 'vue-router';
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import Home from '../views/Home.vue';
import Cart from '../views/Cart.vue';
import Market from '../views/Market.vue';
import Language from '../views/Language.vue';
import Withdraw from '../views/Withdraw.vue';
import WithdrawRecord from '../views/WithdrawRecord.vue';
import Detail from '../views/Detail.vue';
import Deposit from '../views/Deposit.vue';
import Platform from '../views/Platform.vue';
import Address from '../views/Address.vue';
import CustomerService from '../views/CustomerService.vue';
import Password from '../views/Password.vue';
import PasswordFund from '../views/PasswordFund.vue';
import Submit from '../views/Submit.vue';
import My from '../views/My.vue';
import Bank from '../views/Bank.vue';
import DefaultLayout from '@/layouts/DefaultLayout.vue';
import MainLayout from '@/layouts/MainLayout.vue';

const routes = [
    {
        path: '/login',
        name: 'login',
        component: Login, // 登录页使用登录布局
    },
    {
        path: '/language',
        name: 'language',
        component: Language, // 语言设置
    },
    {
        path: '/register',
        name: 'register',
        component: Register,
    },
    {
        path: '/withdraw',
        name: 'Withdraw',
        component: Withdraw,
    },
    {
        path: '/detail',
        name: 'Detail',
        component: Detail,
    },
    {
        path: '/deposit',
        name: 'Deposit',
        component: Deposit,
    },
    {
        path: '/platform',
        name: 'Platform',
        component: Platform,
    },
    {
        path: '/address',
        name: 'Address',
        component: Address,
    },
    {
        path: '/bank',
        name: 'Bank',
        component: Bank,
    },
    {
        path: '/password',
        name: 'Password',
        component: Password,
    },
    {
        path: '/password/assets',
        name: 'PasswordFund',
        component: PasswordFund,
    },
    {
        path: '/submit',
        name: 'Submit',
        component: Submit,
    },
    {
        path: '/withdraw/record',
        name: 'withdrawRecord',
        component: WithdrawRecord
    },
    {
        path: '/',
        component: DefaultLayout, // 主页面使用带底部菜单布局
        children: [
            {
                path: 'nested',
                component: MainLayout, // 嵌套SubLayout的路由
                children: [
                    { path: '/', component: Home },
                    { path: '/task', component: Market },
                ],
            },
            { path: '/cart', component: Cart },
            { path: '/customer-service', component: CustomerService },
            { path: '/my', component: My },
        ],
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})
const whitelist = ['/language', '/register', '/login']
router.beforeEach((to, from, next) => {
    // console.log(to, from)
    const token = localStorage.getItem('token')

    if (to.path == '/login' && token) {
        next('/')
    }
    if (!whitelist.includes(to.path) && !token) {
        next('/login')
    }
    next()
})
export default router